<?php

/**
 * Description of AjaxController
 *
 * @author ZW 2014-8-20
 */

namespace Admin\Controller;

use Think\Controller;

class AjaxController extends Controller {

    //模型字段排序
    public function modelajax() {
        $action = isset($_POST['action']) ? $_POST['action'] : null;
        if ($action === null) {
            echo -1;
            return;
        }


        if ($action == 'fieldorder') {
            $data = isset($_POST['data']) ? $_POST['data'] : null;
            $mid = isset($_POST['mid']) ? $_POST['mid'] : null;
            if ($mid === null || $data === null) {
                echo -1;
                return;
            }


            $m = D('model');
            $arr = $m->field('fieldset')->where('id=' . $mid)->find();
            $arr = unserialize($arr['fieldset']);
            
            $newfieldset = array();
            foreach ($data as $k => $v) {
                $newfieldset[$k] = $arr[$v['index']];
            }
            //dump($newfieldset);
            $field_str = serialize($newfieldset);
            $newdata['fieldset'] = $field_str;
            $newdata['id'] = $mid;
            if ($m->save($newdata) !== false) {
                echo 1;
                return;
            } else {
                echo "排序保存失败!";
                return;
            }
        } else {
            echo -1;
            return;
        }
    }

}

?>
